package protocol.message;

import protocol.ProtocolCode;
import protocol.ProtocolMessage;
import stream.ByteCoding;

/**
 *
 * @author Simão Paulo Rato Alves Reis
 */
public class AccessGranted extends ProtocolMessage {

    private boolean granted;
    private long clientid;

    public AccessGranted(boolean granted, long clientid, AccountAccess accountAccess) {
        super();
        this.granted = granted;
        this.clientid = clientid;
        connectionid = accountAccess.connectionid;
        userid = 0;
    }

    public AccessGranted(byte[] stream) {
        super(stream);
        clientid = ByteCoding.setAsLong(32, stream);
        granted = ByteCoding.setAsBoolean(40, stream);
    }

    @Override
    public String toString() {
        return "Access Granted\n"
                + "Protocol Code: " + ProtocolCode.ACCESS_GRANTED
                + super.toString()
                + "\nClient ID: " + clientid
                + "\nGranted: " + granted;
    }

    @Override
    public byte[] getBytes() {
        return ByteCoding.concat(
                ProtocolCode.ACCESS_GRANTED.getBytes(),
                ByteCoding.padding(7),
                super.getBytes(),
                ByteCoding.get(clientid),
                ByteCoding.get(granted));
    }

    public boolean granted() {
        return granted;
    }
}
